import { fileURLToPath, URL } from 'node:url'

import { loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { NaiveUiResolver } from 'unplugin-vue-components/resolvers'
import type { UserConfig, ConfigEnv } from 'vite'

import prismjs from 'vite-plugin-prismjs'
// https://vite.dev/config/
export default ({ mode }: ConfigEnv): UserConfig => {
  const root: string = process.cwd()
  const env = loadEnv(mode, root)
  return {
    plugins: [
      vue(),
      AutoImport({
        resolvers: [ElementPlusResolver()],
        imports: [
          'vue',
          {
            'naive-ui': ['useDialog', 'useMessage', 'useNotification', 'useLoadingBar'],
          },
        ],
      }),
      Components({
        resolvers: [ElementPlusResolver({ importStyle: 'sass' }), NaiveUiResolver()],
      }),
      prismjs({
        languages: ['json', 'html', 'css', 'javascript', 'typescript', 'markdown', 'bash', 'scss','java','python','go','php','sql','shell','yaml','xml','nginx','dockerfile','ini','less','stylus','ruby','swift','kotlin','rust','dart','lua','groovy','scala','perl','r','haskell','erlang','clojure','scheme','lisp','fsharp','powershell'],
        plugins: ['line-numbers', 'show-language'],
      }),
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url)),
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@use "/src/style/variable.scss" as *;',
        },
      },
    },
    // 代理跨越
    server: {
      proxy: {
        [env.VITE_APP_BASE_API]: {
          // 网络路径
          target: env.VITE_SERVE,
          // 开启跨域
          changeOrigin: true,
          // 路径重写
          rewrite: (path: any) => path.replace(/^\/api/, ''),
        },
      },
    },
  }
}
